package com.company.Thread.Concurrent;

import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;

public class LockList {
    public static void main(String[] args) {
        /**
         * List<String> list = new ArrayList<>();
         * 并发会发生java.util.ConcurrentModificationException并发修改异常
         */
        /**
         * 解决方案：
         * 1、List<String> list = new Vector<>();
         * Vector是并发安全的
         *
         * 2、List<String> list = Collections.synchronizedList(new ArrayList<>());
         * 3、List<String> list = new CopyOnWriteArrayList<>();
         */

        /*
        CopyOnWrite 写入时复制
         */
        List<String> list = new CopyOnWriteArrayList<>();

        for(int i=0;i<=20;i++){
            new Thread(()->{
                list.add(UUID.randomUUID().toString().substring(0,5));
                System.out.println(list);
            }, String.valueOf(i)).start();
        }
    }
}
